<template>
  <div class="login-container">
    <div class="login-box">
      <div class="img-box">
        <img src="../assets/logo.png" alt="">
      </div>
      <el-form :rules="LoginRules" :model="loginForm" label-width="0px" class="login-form" ref="loginValidateForm">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" v-model="loginForm.password" prefix-icon="iconfont icon-3702mima"></el-input>
        </el-form-item>
        <el-form-item class="login-reset">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      LoginRules: {
        username: [
          { required: true, message: '请输入用户名称', trigger: 'blur' },
          { min: 2, max: 8, message: '长度在 2 到 8 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入用户密码', trigger: 'blur' },
          { min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetForm() {
      this.$refs.loginValidateForm.resetFields()
    },
    login() {
      this.$refs.loginValidateForm.validate(async (valid) => {
        if(!valid) return console.log('error submit!!')

        const { data: res } = await this.$http.post('login', {
          username: this.loginForm.username,
          password: this.loginForm.password
        })

        if(res.meta.status !== 200) return this.$message.error(res.meta.msg)

        this.$message.success(res.meta.msg)

        sessionStorage.setItem('token', res.data.token)

        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
  .login-container{
    background: #2b4b6b;
    height: 100%;
    .login-box{
      width: 450px;
      height: 300px;
      background: #fff;
      border-radius: 3px;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
    }
    .img-box{
      width: 130px;
      height: 130px;
      border: 1px solid #eee;
      border-radius: 50%;
      padding: 8px;
      box-shadow: 0 0 10px #eee;
      position: absolute;
      background-color: #fff;
      left: 50%;
      transform: translateX(-50%) translateY(-50%);
      img{
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background: #eee;
      }
    }
    .login-form{
      position: absolute;
      bottom: 0;
      width: 100%;
      padding: 0 20px;
      box-sizing: border-box;
      .login-reset{
        display: flex;
        justify-content: flex-end;
      }
    }
  }
</style>